<?php
class PackageController extends CBaseController
{
	protected $package;
	protected $validator;
	
	public function __construct($config=null) 
    { 
      parent::__construct($config);	  
    } 
	
	public function init($tpl)
	{
		parent::init($tpl);
		$this->reqLogin();
		$this->templator->addViewsDir(dirname(__FILE__).'/../views');
		$this->templator->assign('menu','general');
	}
	
	public function all()
	{
	 $packages = R::findAll('travelpackage',' ORDER BY name');
	 $this->templator->assign('packages',array_merge($packages, array()));
	 $this->templator->display('package_list.html');
	}
	
	public function savePrepare()
	{
		$this->package = R::dispense('travelpackage');
		$this->package->name = $_POST['name'];
		$this->package->packagecode = $_POST['packagecode'];
		$this->package->shortdesc =  $_POST['shortdesc'];
		$this->package->longdesc = $_POST['longdesc'];
		$this->package->type = $_POST['type'];
		$this->package->nights = $_POST['nights'];
		$this->package->days = $_POST['days'];
		$this->package->tourplan = $_POST['tourplan'];
		$this->package->startingcity = $_POST['startingcity'];
		$this->package->highlight = $_POST['highlight'];
		$this->package->price = $_POST['price'];
		$this->package->finalprice = $_POST['finalprice'];
		$this->package->pricequote = $_POST['pricequote'];
		$this->package->seoname = $_POST['seoname'];
		if(isset($_POST['todate']) && !empty($_POST['todate']))
		{
			$this->package->todate = date("Y-m-d",strtotime($_POST['todate']));
		}
		if(isset($_POST['fromdate']) && !empty($_POST['fromdate']))
		{
			$this->package->fromdate = date("Y-m-d",strtotime($_POST['fromdate']));
		}
		$this->package->validquote = $_POST['validquote'];		
		for ( $counter = 1; $counter <= $this->package->days ; $counter += 1) 
		{
			$itinerary = R::dispense('itinerary');
			$itinerary->day=$counter;
			$this->package->ownItinerary[$counter]=$itinerary;
		}
	}
	
	public function updatePrepare()
	{
		$id = $_POST['id'];
		$this->package = R::load('travelpackage',$id);
		$this->package->name = $_POST['name'];
		$this->package->packagecode = $_POST['packagecode'];
		$this->package->shortdesc =  $_POST['shortdesc'];
		$this->package->longdesc = $_POST['longdesc'];
		$this->package->type = $_POST['type'];
		$this->package->nights = $_POST['nights'];
		$this->package->days = $_POST['days'];
		$this->package->tourplan = $_POST['tourplan'];
		$this->package->startingcity = $_POST['startingcity'];
		$this->package->highlight = $_POST['highlight'];
		$this->package->price = $_POST['price'];
		$this->package->finalprice = $_POST['finalprice'];
		$this->package->pricequote = $_POST['pricequote'];
		$this->package->seoname = $_POST['seoname'];
		if(isset($_POST['todate']) && !empty($_POST['todate']))
		{
			$this->package->todate = date("Y-m-d",strtotime($_POST['todate']));
		}
		if(isset($_POST['fromdate']) && !empty($_POST['fromdate']))
		{
			$this->package->fromdate = date("Y-m-d",strtotime($_POST['fromdate']));
		}
		$this->package->validquote = $_POST['validquote'];		
				
	}
	public function updateItinenery()
	{
		if(!isset($_POST['daystitle']))
			return ;
		$itinry_title_arr = $_POST['daystitle'];
		$itinry_desc_arr = $_POST['dayslongdesc'];
		foreach($itinry_title_arr as $key => $value)
		{
			if($this->package->ownItinerary[$key]->day > $this->package->days)
			{	
				$it=R::load('itinerary',$key);
				R::trash($it);
				continue;
			}		
			$this->package->ownItinerary[$key]->title=$value;
			$this->package->ownItinerary[$key]->longdesc=$itinry_desc_arr[$key];
			
		}
		$counte=count($this->package->ownItinerary);
		for($i=$counte+1; $i<=$this->package->days; $i=$i+1)
		{
			$itinerary = R::dispense('itinerary');
			$itinerary->day=$i;
			$this->package->ownItinerary[]=$itinerary;
		}
		R::store($this->package);
	}
	public function saveORupdateValidate()
	{
		$this->validator = new FormValidator();
		$this->validator->addValidation("name","req","Please fill in Name");
		return $this->validator->ValidateForm();
	}
		
	
	public function create()
	{
		$this->package = R::dispense('travelpackage');
		$this->templator->assignByRef('package',$this->package);
		$this->templator->display('package_general.html');
	}
	public function save()
	{
	  $this->savePrepare();
	  if($this->saveORupdateValidate())
	  {
		R::store($this->package);
		$this->templator->assign('info','Package added sucessfully');
		$this->templator->assignByRef('package',$this->package);		
		$this->templator->display('package_general.html');	
	  }
	  else
	  {
		$error_hash = $this->validator->GetErrors();
		$this->templator->assign('validate_errors',$error_hash);
		$this->templator->assignByRef('package',$this->package);
		$this->templator->display('package_general.html');
	  }
	}
	
	public function edit()
	{
		$id = $_GET['id'];
		if(isset($id))
		$this->package = R::load('travelpackage',$id);
		else
		$this->package = R::dispense('travelpackage');
		$this->templator->assignByRef('package',$this->package);		
		$this->templator->display('package_general.html');
	}
	
	public function update()
	{
		$this->updatePrepare();
		  if($this->saveORupdateValidate())
		  {
			R::store($this->package);
			$this->updateItinenery();
			$this->templator->assignByRef('package',$this->package);
			$this->templator->assign('info','Package updated sucessfully');
			$this->templator->display('package_general.html');	
		  }
		  else
		  {
			$error_hash = $this->validator->GetErrors();
			$this->templator->assign('validate_errors',$error_hash);
			$this->templator->assignByRef('package',$this->package);
			$this->templator->display('package_general.html');
		  }
	}
	
	public function delete()
	{
	 $id = $_GET['id'];
	 if(isset($id))
	 {
		$package = R::load('travelpackage',$id);
		if(isset($package))
		{
			R::dependencies(array('itinerary'=>array('travelpackage'),'inclusion'=>array('travelpackage')));
			R::trash($package);
			$this->templator->assign('info','Package deleted sucessfully');
		}
	 }
	 $this->all();
	}
}	